package com.tmeone.erpone.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.Product;
import com.tmeone.erpone.entity.TProduct;
import com.tmeone.erpone.service.ITProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.tmeone.erpone.entity.SUser;
import com.tmeone.erpone.service.ITProductService;
import com.tmeone.erpone.util.MyUser;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tmeone
 * @since 2021-04-19
 */
@RestController
public class TProductController {

    @Autowired
    private ITProductService productService;

    @Autowired
    private MyUser myUser;

    //查询所有商品
    @GetMapping("/orderProduct")
    public JsonResult getOrderProduct(){
        List<TProduct> list = productService.list();
        return new JsonResult(1,list);
    }

    @GetMapping("/products")
    public JsonResult pageProducts(@RequestParam(value = "curr",required = false,defaultValue = "1") Integer current,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){
            Page<Product> arg = new Page<>(current, pageSize);
            IPage<Product> page = productService.pageProducts(arg);
            return new JsonResult(1,page);
    }
    //根据company_id查询所有商品
    @GetMapping("/products/{id}")
    public JsonResult products(@PathVariable String id){
        List<TProduct> products = productService.getProductByCustomerId(id);
        return new JsonResult(1,products);
    }

    /**
     * 获取供应商商品
     * 作者：wzs
     *
     * @param search    查询关键字
     * @param companyId 供应商公司id
     * @param current   当前页
     * @return 供应商商品
     */
    @GetMapping("/supplierProducts")
    public JsonResult supplierProducts(@RequestParam(value = "search", required = false, defaultValue = "") String search,
                                       @RequestParam(value = "companyId", required = false, defaultValue = "") String companyId,
                                       @RequestParam(value = "current", required = false, defaultValue = "1") int current) {
        Page page = new Page(current, 5);
        if (companyId.equals("")) {
            // 获取当前用户
            SUser user = myUser.getUser();

            // 查询所有供应商商品
            return new JsonResult(1, productService.selectSupplierAllProductPage(page, search, user.getCompanyId()));
        } else {
            return new JsonResult(1, productService.selectSupplierProductPage(page, search, companyId));
        }
    }
}
